package com.jxbeidou.generate.controller;

import com.jxbeidou.core.constents.ValidateConstants;
import com.jxbeidou.core.encrypt.annotation.Decrypt;
import com.jxbeidou.core.entity.PageResult;
import com.jxbeidou.core.entity.R;
import com.jxbeidou.core.validate.ModifyValidateGroup;
import com.jxbeidou.core.validate.SaveValidateGroup;
import com.jxbeidou.generate.dto.DatabaseConnectDTO;
import com.jxbeidou.generate.dto.DatabaseConnectSaveDTO;
import com.jxbeidou.generate.dto.SyncTableDTO;
import com.jxbeidou.generate.query.DatabaseConnectQueryDTO;
import com.jxbeidou.generate.query.page.DatabaseConnectPageQueryDTO;
import com.jxbeidou.generate.service.DatabaseConnectService;
import com.jxbeidou.generate.vo.DatabaseConnectVO;
import com.jxbeidou.generate.vo.TableInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: jxbeidou-basic
 * @ClassName: DatabaseConnectController
 * @description: 数据库连接管理
 * @author: 杜晓祥
 * @create: 2024-03-13 11:36
 * 版权所有：交信北斗(北京)信息科技有些公司
 */
@Slf4j
@RestController
@RequestMapping("/generate/database-connect")
@Tag(name = "代码生成项目管理")
public class DatabaseConnectController {
    
    @Resource
    private DatabaseConnectService databaseConnectService;


    @PostMapping("/page")
    @Operation(summary = "分页查询")
    public R<PageResult<DatabaseConnectVO>> page(@RequestBody(required = false) DatabaseConnectPageQueryDTO query){
        PageResult<DatabaseConnectVO> page = databaseConnectService.page(query);
        return R.success(page);
    }

    @PostMapping("/list")
    @Operation(summary = "查询所有")
    public R<List<DatabaseConnectVO>> list (@RequestBody(required = false) DatabaseConnectQueryDTO query) {
        return R.success(databaseConnectService.list(query));
    }

    @PostMapping("/test-connect")
    @Operation(summary = "测试连接")
    @Decrypt(DatabaseConnectDTO.class)
    public R<String> testConnect (DatabaseConnectDTO dto) {
        databaseConnectService.testConnect(dto);
        return R.success("连接成功");
    }

    @PostMapping("/findByTable/{id}")
    public R<List<TableInfoVO>> findByTable(@PathVariable("id") @NotNull(message = "没有获取到数据库ID") Long id) {
        return R.success(databaseConnectService.findByTable(id));
    }


    @PostMapping("/sync")
    @Operation(summary = "同步表信息")
    public R<String> syncTable (@RequestBody SyncTableDTO table) {
        databaseConnectService.syncDatabase(table);
        return R.success("同步成功");
    }

    @GetMapping("/{id}")
    @Operation(summary = "ID查询")
    public R<DatabaseConnectVO> get(@PathVariable("id") @NotNull(message = ValidateConstants.FIND_ID_NULL) Long id){
        return R.success(databaseConnectService.findById(id));
    }

    @PostMapping
    @Operation(summary = "新增")
    public R<String> save(@RequestBody @Validated(SaveValidateGroup.class) DatabaseConnectSaveDTO vo){
        databaseConnectService.save(vo);
        return R.saveSuccess();
    }

    @PutMapping
    @Operation(summary = "修改")
    public R<String> update(@RequestBody @Validated(ModifyValidateGroup.class) DatabaseConnectSaveDTO vo){
        databaseConnectService.modify(vo);
        return R.modifySuccess();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "ID删除")
    public R<String> delete(@PathVariable("id") @NotNull(message = ValidateConstants.DELETE_ID_NULL) Long id){
        databaseConnectService.delete(id);
        return R.deleteSuccess();
    }

    @PutMapping("/delete")
    @Operation(summary = "批量删除")
    public R<String> delete(@RequestBody @NotEmpty(message = ValidateConstants.DELETE_IDS_NULL) List<Long> idList) {
        databaseConnectService.deleteForList(idList);
        return R.deleteSuccess();
    }
    
}
